﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace blackhouse.BlackApart.Data.Caching {
	public class CachedCompanyDataAccess : ICompanyDataAccess {

		#region Fields

		private ICompanyDataAccess decoratedDataAccess;
		private NemoCache cache;

		#endregion

		#region Constructors

		public CachedCompanyDataAccess(ICompanyDataAccess decoratedDataAccess, string connectionStringToDecoretedDb) {
			this.decoratedDataAccess = decoratedDataAccess;
			this.cache = new NemoCache(TimeSpan.FromMinutes(2), connectionStringToDecoretedDb);
		}

		#endregion

		#region Methods

		public CompanyCountsData GetCompanyCounts() {
			cache.Reset();
			cache.SetDependencyTables("T34", "T105", "T51", "T117", "T117_T125", "T125", "T51_T43", "T43", "T19");
			cache.SetKey("GetCompanyCounts");
			CompanyCountsData data = cache.GetData<CompanyCountsData>();
			if (data.Clients == 0) {
				data = this.decoratedDataAccess.GetCompanyCounts();
				cache.SetCaching(data);
			}
			return data;
		}

		public ApartmentPromotionBanerData[] GetBanners(string language) {
			cache.Reset();
			cache.SetDependencyTables("T225");
			cache.SetParameters(language);
			cache.SetKey("GetBanners");
			ApartmentPromotionBanerData[] data = cache.GetData<ApartmentPromotionBanerData[]>();
			if (data == null) {
				data = this.decoratedDataAccess.GetBanners(language);
				cache.SetCaching(data);
			}
			return data;
		}

		public LanguageData[] GetLanguages() {
			cache.Reset();
			cache.SetDependencyTables("T243");
			cache.SetKey("GetLanguages");
			LanguageData[] data = cache.GetData<LanguageData[]>();
			if (data == null) {
				data = this.decoratedDataAccess.GetLanguages();
				cache.SetCaching(data);
			}
			return data;
		}

		#endregion

	}
}
